package app.caihan.wanandroid.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import app.caihan.wanandroid.R
import app.caihan.wanandroid.databinding.FragmentLauncherUiBinding
import app.caihan.wanandroid.ui.find.FindKnowledgeFragment
import app.caihan.wanandroid.ui.main.MainFragment
import app.caihan.wanandroid.ui.me.MeFragment
import app.caihan.wanandroid.ui.wxarticle.WxArticleFragment
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 *
 *
 * @author Seven-C
 * @date 2020/8/11
 */
class LauncherUiFragment :Fragment(){

    lateinit var mBinding: FragmentLauncherUiBinding
    private val navView by lazy { mBinding.navView }
    private val mainViewpager by lazy { mBinding.mainViewpager }

    private val fragmentList = arrayListOf<Fragment>()
    private val mainFragment by lazy { MainFragment.newInstance(0) }
    private val systemFragment by lazy { FindKnowledgeFragment.newInstance() }
    private val blogFragment by lazy { WxArticleFragment.newInstance() }
    private val projectFragment by lazy { MainFragment.newInstance(3) }
    private val peopleFragment by lazy { MeFragment.newInstance() }

    init {
        fragmentList.run {
            add(mainFragment)
            add(systemFragment)
            add(blogFragment)
            add(projectFragment)
            add(peopleFragment)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_launcher_ui, container, false)
        mBinding.lifecycleOwner = this
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initViewPager()
        navView.setOnNavigationItemSelectedListener(onNavigationItemSelected)
        super.onViewCreated(view, savedInstanceState)
    }

    private val onNavigationItemSelected = BottomNavigationView.OnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.home -> {
                switchFragment(0)
            }
            R.id.system -> {
                switchFragment(1)
            }
            R.id.blog -> {
                switchFragment(2)
            }
            R.id.project -> {
                switchFragment(3)
            }
            R.id.people -> {
                switchFragment(4)
            }
        }
        true
    }

    private fun switchFragment(position: Int): Boolean {
//        mainViewpager.currentItem = position
        mainViewpager.setCurrentItem(position, false)
        return true
    }

    private fun initViewPager() {
        mainViewpager.isUserInputEnabled = false//设置是否允许用户输入/触摸
//        mainViewpager.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        mainViewpager.offscreenPageLimit = 2//设置屏幕外加载页面数量
        mainViewpager.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int) = fragmentList[position]

            override fun getItemCount() = fragmentList.size
        }
    }
}